package br.com.colegio.dao;

import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import br.com.colegio.dao.storage.Storage;
import br.com.colegio.vo.Lancamento;

public class LancamentoDAO
{
	public static Collection<Object> list()
	{
		try
		{
			return Storage.select(Lancamento.class.getName(), null);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

		return null;
	}

	public static Lancamento get(Long cod)
	{
		try
		{
			Object res = Storage.get(Lancamento.class.getName(), cod);

			if (res != null)
				return (Lancamento)res;
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

		return null;
	}

	public static Collection<Object> getByData(Date dIni, Date dFim)
	{
		try
		{
			String params = "String p1, String p2";
			String filter = "";

			if (dIni != null)
				filter = "data >= p1";

			if (dFim != null && !filter.equals(""))
				filter = "&& data < p2";
			else
				filter = "data < p2";

			Map<String, Object> parVal = new HashMap<String, Object>();
			parVal.put("p1", dIni);
			parVal.put("p1", dFim);

			return Storage.select(Lancamento.class.getName(), filter, params, parVal, null);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

		return null;
	}

	public static Lancamento save(Lancamento reg) throws Exception
	{
		return (Lancamento)Storage.save(reg);
	}

	public static void delete(Long reg) throws Exception
	{
		Storage.delete(Lancamento.class.getName(), reg);
	}
}
